/*
 * 
 */
package com.lihao.demo1.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.graphics.Xfermode;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.ImageView;

import com.lihao.demo1.R;


/**
 * 自定义ImageView，实现圆形图片.
 */
public class CircleImageView extends ImageView {

    /** 转换器. */
    private static final Xfermode MASK_XFERMODE;

    /** The Mask Bitmap. */
    private Bitmap mMask;

    /** 画板. */
    private Paint mPaint;

    static {
        // 取两层绘制交集。显示下层。
        PorterDuff.Mode localMode = PorterDuff.Mode.DST_IN;
        MASK_XFERMODE = new PorterDuffXfermode(localMode);
    }

    /**
     * 构造函数.
     * 
     * @param paramContext
     *            the param context
     */
    public CircleImageView(final Context paramContext) {
        super(paramContext);
    }

    /**
     * 构造函数.
     * 
     * @param paramContext
     *            the param context
     * @param paramAttributeSet
     *            the param attributeSet
     */
    public CircleImageView(final Context paramContext, final AttributeSet paramAttributeSet) {
        super(paramContext, paramAttributeSet);
    }

    /**
     * 构造函数.
     * 
     * @param paramContext
     *            the param context
     * @param paramAttributeSet
     *            the param attributeSet
     * @param paramInt
     *            the param int
     */
    public CircleImageView(final Context paramContext, final AttributeSet paramAttributeSet, final int paramInt) {
        super(paramContext, paramAttributeSet, paramInt);
    }

    /**
     * 重写OnDraw， 画图片为圆形.
     * 
     * @param paramCanvas
     *            the param canvas
     */
    protected final void onDraw(final Canvas paramCanvas) {
        if (this.isInEditMode()){
            super.onDraw(paramCanvas);
            return;
        }
        Drawable localDrawable = getDrawable();
        if (localDrawable == null) {
            return;
        }
        try {
            if (this.mPaint == null) {
                Paint localPaint1 = new Paint();
                this.mPaint = localPaint1;
                this.mPaint.setFilterBitmap(false);
                Paint localPaint2 = this.mPaint;
                Xfermode localXfermode1 = MASK_XFERMODE;
                @SuppressWarnings("unused")
                Xfermode localXfermode2 = localPaint2.setXfermode(localXfermode1);
            }
            float f1 = getWidth();
            float f2 = getHeight();
            int i = paramCanvas.saveLayer(0.0F, 0.0F, f1, f2, null, Canvas.ALL_SAVE_FLAG);
            int j = getWidth();
            int k = getHeight();
            localDrawable.setBounds(0, 0, j, k);
            localDrawable.draw(paramCanvas);
            if ((this.mMask == null) || (this.mMask.isRecycled())) {
                Bitmap localBitmap1 = createMask();
                this.mMask = localBitmap1;
            }
            Bitmap localBitmap2 = this.mMask;
            Paint localPaint3 = this.mPaint;
            paramCanvas.drawBitmap(localBitmap2, 0.0F, 0.0F, localPaint3);
            paramCanvas.restoreToCount(i);
            return;
        } catch (Exception localException) {
            StringBuilder localStringBuilder = new StringBuilder()
                    .append("Attempting to draw with recycled bitmap. View ID = ");
            System.out.println("localStringBuilder==" + localStringBuilder);
        }
    }

    /**
     * Creates the mask.
     * 
     * @return the bitmap
     */
    public final Bitmap createMask() {
        //final int color = -16777216;
        int i = getWidth();
        int j = getHeight();
        Bitmap.Config localConfig = Bitmap.Config.ARGB_8888;
        Bitmap localBitmap = Bitmap.createBitmap(i, j, localConfig);
        Canvas localCanvas = new Canvas(localBitmap);
        Paint localPaint = new Paint(1);
        localPaint.setColor(getResources().getColor(R.color.circle_mark_color));
        float f1 = getWidth();
        float f2 = getHeight();
        RectF localRectF = new RectF(0.0F, 0.0F, f1, f2);
        localCanvas.drawOval(localRectF, localPaint);
        return localBitmap;
    }
}